/*jslint browser: true*/
/*global $, jQuery, alert, window*/
window.view = {};
window.view.set = {
    "train_url": function (major_id) {
        "use strict";
        $("#train_url").attr("href", "/major/{id}/index/".format({
            id: major_id
        }));
    },
    "paper_url": function (major_id) {
        "use strict";
        $("#paper_url").attr("href", "/major/{id}/paper/".format({
            id: major_id
        }));
    },
    "history_url_train": function (major_id) {
        "use strict";
        $("#history_url_train").attr("href", "/major/{id}/history/train/".format({
            id: major_id
        }));
    },
    "history_url_paper": function (major_id) {
        "use strict";
        $("#history_url_paper").attr("href", "/major/{id}/history/paper/".format({
            id: major_id
        }));
    },
    "major_title": function (major_name) {
        "use strict";
        $("#major_title").html(major_name);
    },
    "major_crumb": function (major_name) {
        "use strict";
        $("#major_crumb").html(major_name);
    }
};
window.sync = {
    "paper_list_html": function (paper_list) {
        "use strict";
        var index,
            length = paper_list.length,
            paper_item,
            time,
            tr_length = 4,
            detail = [];
        for (index = 0; index < length; index = index + 1) {
            paper_item = paper_list[index];
            time = index % tr_length;
            if (time === 0) {
                detail.push("<table><tr>");
            }
            detail.push('<td class="item">');
            detail.push('<a paper_id={paper_id} href="javascript:void(0)">'.format({
                paper_id: paper_item.id
            }));
            detail.push('<div></div><span>{major_name}</span>'.format({
                major_name: paper_item.name || "&emsp;"
            }));
            detail.push('<div class="neg unbook">开始考试</div></a></td>');
            if (time !== 3) {
                detail.push('<td class="item_none"></td>');
            } else {
                detail.push('</tr></table><div class="space"></div>');
            }
        }
        $("#all_paper").html("");
        $(detail.join("")).appendTo("#all_paper");
    }
};
window.init = {
    "global_var": function () {
        "use strict";
        var pathname = location.pathname,
            major_key,
            update_reg = new RegExp('^/major/([0-9]+)/paper/$'),
            reg_result_list = update_reg.exec(pathname),
            major_list,
            index,
            length,
            major_item;
        if (reg_result_list) {
            //update question
            window.is_valid = true;
            window.paper_all_key = "/paper/";
            window.major_id = parseInt(reg_result_list[1], 10);
            major_key = "/major/{id}/".format({id: window.major_id});
            major_item = window.format.s2c(window.storage(major_key),
                                           window.format.major)[0];
            window.major_name =  major_item.name;
        } else {
            throw new Error("网页错误");
        }
    },
    "html_page": function () {
        "use strict";
        window.view.set.train_url(window.major_id);
        window.view.set.paper_url(window.major_id);
        window.view.set.history_url_train(window.major_id);
        window.view.set.history_url_paper(window.major_id);
        window.view.set.major_title(window.major_name);
        window.view.set.major_title(window.major_name);
    },
    "paper_list": function () {
        "use strict";
        var search_data = {
                "major_id": window.major_id,
                "is_active": true,
                "category_num": 2
            };
        $.ajax({
            url:  window.paper_all_key,
            type: "PUT",
            dataType: "json",
            async: false,
            data: JSON.stringify(search_data),
            success: function (result, status) {
                var paper_list = window.format.s2c(result.data,
                                                   window.format.paper);
                window.sync.paper_list_html(paper_list);
                window.hideLoading();
            }
        });
    },
    "addEventListener": function () {
        "use strict";
        $(".s_paper_index").delegate("a", "click",
                                     window.listener.start_exam);
    }
};
window.init_func_list = ["global_var",
                        "html_page",
                        "paper_list",
                        "addEventListener"];
window.listener = {
    "start_exam": function () {
        "use strict";
        window.loading();
        var element = $(this),
            paper_id = parseInt(element.attr("paper_id"), 10),
            paper_key = "/paper/{id}/".format({id: paper_id}),
            paper_data = window.format.s2c(window.storage(paper_key),
                                           window.format.paper)[0],
            detail_key = "/paper/detail/{id}/".format({id: paper_data.detail_id}),
            detail_data = window.format.s2c(window.storage(detail_key),
                                            window.format.detail)[0],
            exam_create_data = {
                "detail_id": detail_data.id,
                "major_id": paper_data.major_id,
                "paper_id": paper_data.id,
                "name": paper_data.name,
                "time_limit": paper_data.time_limit,
                "category_num": paper_data.category_num
            };
        $.ajax({
            url:  "/exam/",
            type: "POST",
            dataType: "json",
            async: false,
            data: JSON.stringify(exam_create_data),
            success: function (result, status) {
                var exam_data = window.format.s2c(result.data,
                                                  window.format.exam)[0];
                window.exam_key = "/exam/{exam_id}/".format({
                    exam_id: exam_data.id
                });
                window.exam_url = "/exam/{exam_id}/exam/".format({
                    exam_id: exam_data.id
                });
                window.storage(window.exam_key, result.data);
                window.open(window.exam_url);
                window.hideLoading();
            }
        });
    }
};
